package com.zl.project.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.ModelAndView;

import com.zl.project.pojo.User;
import com.zl.project.service.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
    @Autowired
   private UserService userService;
    
//    登录用户,成功返回首页,失败返回登录页
	@RequestMapping(value="login",method=RequestMethod.POST)
   public String login(String username,String pwd,HttpServletRequest request,Model model) {
		
		User user=new User();
		user.setUsername(username);
		user.setPwd(pwd);
		
		/*String username=user.getUsername();
		System.out.println("asdfads"+username);
		String pwd1 =user.getPwd();
		System.out.println("jausdh"+user);*/
		User U=userService.login(user);
       if(U!=null) {
    	   //登录成功,将数据存到session中
    	   request.getSession().setAttribute("LoginUser", U);;
    	   return "index";
       }		
	return "login";
	
	     
	   
   }
	
//注册页
	@RequestMapping(value="register",method=RequestMethod.POST)
	public String register(String username,String pwd,String phone,String email,Model model) {
		User user=new User();
		user.setUsername(username);
		user.setPwd(pwd);
		user.setEmail(email);
		user.setPhone(phone);
		
		userService.register(user);
		
		
		return "login";
	}
}
